package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class setMealController {


    @Autowired
    SetMealService setMealService;

    /**
     * 新增套餐
     *
     * @param SetmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#SetmealDTO.categoryId")//key:setmealCache::categoryId
    public Result save(@RequestBody SetmealDTO SetmealDTO) {
        log.info("新增套餐，参数为{}", SetmealDTO);
        setMealService.saveWithDish(SetmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询，参数为{}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐
     *
     * @param
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//key:setmealCache::setmealId
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐，参数为{}", ids);
        setMealService.delete(ids);
        return Result.success();
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询套餐")
    public Result<SetmealVO> selectById(@PathVariable Long id) {
        log.info("批量删除套餐，参数为{}", id);
        SetmealVO setmealVO = setMealService.selectById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//key:setmealCache::setmealId
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐，参数为{}", setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐的起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//key:setmealCache::setmealId
    public Result StartOrStop(@PathVariable Integer status ,Long id ) {
        log.info("套餐的起售停售，参数为{},{}", status,id);
        setMealService.StartOrStop(status,id);
        return Result.success();
    }

}
